package com.cdl.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cdl.utils.bean.ColumnInfo;
import com.cdl.utils.bean.IndexInfo;

/**
 * 根据数据库表信息自动生成一组对表的增删改查操作代码;
 * 其中包括javaBean、controller、Service、ServiceImpl、Mapper、sqlMap、Jsp(main.jsp、query.jsp、list.jsp、form.jsp、detail.jsp)
 * @param args
 */
public class CodeUtil {
    private static Logger logger = LoggerFactory.getLogger(CodeUtil.class);
	private String workSpaceRoot ;   //当前项目工作空间主目录
    private String tableName  ;      //表名称
    private String[] primaryCol ;      //主键（列名）
    private String[] primaryKey ;      //主键（属性名）
    
    private String className  ;		 //类名称
    private String _className ;      //类别名
    private String controllerName ;  //controller路径别名
    private String mapperName ;      //dao层接口名称
    private String serviceName ;     //service层接口名称
    private String serviceImplName ; //service层接口实现类名称
    private String sqlMapName ;      //sqlMap文件名
    private List<ColumnInfo> cols ;  //数据库表字段信息
    private List<IndexInfo> indexs ; //数据库表索引信息
	private FileWriter fw = null ;   //生成文件流
	private PrintWriter pw= null ;   
    
	/***
	 * 构造自动生成代码工具类
	 * @param tableName    表名
	 * @param primaryCol   主键列名
	 */
    public CodeUtil(String tableName){
    	logger.info("构建代码生成工具对象-->");
    	this.tableName = tableName ;
    	//当前项目工作空间主目录
    	this.workSpaceRoot = System.getProperty("user.dir");
    	//读取数据库表所有字段信息
    	this.cols = initColumnInfo(JDBCUtil.getTableColumns(tableName));
    	//读取数据库索引信息
    	this.indexs = JDBCUtil.getTableIndexs(tableName);
    	//根据唯一索引获取主键列
    	for(IndexInfo ind:this.indexs){
    		if(ind.isUnique()){
    			this.primaryCol = ind.getColNames().split(",");
    			this.primaryKey = ind.getColNames().split(",");
    			for(int a=0;a<this.primaryKey.length;a++){
    				this.primaryKey[a] = StringUtil.getFiledString(primaryKey[a]);
    			}
    			break;
    		}
    	}
    	//规范类名
    	this.className = StringUtil.getClassString(tableName);
    	this._className = StringUtil.getFirstCharLower(className);
    	this.controllerName = className+"Controller";
    	this.mapperName = className+"Mapper";
    	this.serviceName = className+"Service";
    	this.serviceImplName = className+"ServiceImpl";
    	this.sqlMapName = StringUtil.getFirstCharLower(className)+"-mapper";
    }
    
    /***
     * 生成文件
     * @param file
     * @param contect
     */
    public void createFile(String dir,String fileName,String contect){
		try {
			File file = new File(workSpaceRoot+dir);
			if(!file.exists()){
				file.mkdirs();
			}
			fw = new FileWriter(new File(workSpaceRoot+dir+fileName));
			pw = new PrintWriter(fw);
			pw.println(contect);
			pw.flush();
		} catch (IOException e) {
			logger.info("创建文件 "+ workSpaceRoot+dir+fileName +" 失败-->异常："+e);
			e.printStackTrace();
		}finally{
			closeWrite(fw,pw);
		}
    }
    
    /***
     *   生成JavaBean对象
     */
	public void createEntity(){
		String dir = "\\src\\main\\java\\com\\cdl\\entity\\";
		String fileName = className+".java";
		String content = parseEntity() ;
		createFile(dir,fileName,content);
		logger.info("成功创建javaBean-->"+dir+fileName);
	}

	/***
	 *   生成控制层：XXXController.java
	 */
	public void createController(){
		String dir = "\\src\\main\\java\\com\\cdl\\controller\\";
		String fileName = controllerName+".java";
		String content = parseController() ;
		createFile(dir,fileName,content);
		logger.info("成功创建controller-->"+dir+fileName);
	}
	
	/***
	 *   生成Service层接口：XXXService.java
	 */
	public void createService(){
		String dir = "\\src\\main\\java\\com\\cdl\\service\\";
		String fileName = serviceName+".java";
		String content = parseService() ;
		createFile(dir,fileName,content);
		logger.info("成功创建Service-->"+dir+fileName);
	}
	
	/***
	 *   生成Service层接口实现：XXXServiceImpl.java
	 */
	public void createServiceImple(){
		String dir = "\\src\\main\\java\\com\\cdl\\service\\impl\\";
		String fileName = serviceImplName+".java";
		String content = parseServiceImpl() ;
		createFile(dir,fileName,content);
		logger.info("成功创建ServiceImpl-->"+dir+fileName);		
	}
	
	/***
	 * 	   生成dao层：XXXMapper.java
	 */
	public void createMapper(){
		String dir = "\\src\\main\\java\\com\\cdl\\dao\\";
		String fileName = mapperName+".java";
		String content = parseMapper() ;
		createFile(dir,fileName,content);
		logger.info("成功创建Mapper-->"+dir+fileName);		
	}
	
	/***
	 *   生成xxx-Mapper.xml文件
	 */
	public void createSqlMap(){
		String dir = "\\src\\main\\resources\\sqlmap\\";
		String fileName = sqlMapName+".xml";
		String content = parseSqlMap() ;
		createFile(dir,fileName,content);
		logger.info("成功创建sqlMap-->"+dir+fileName);				
	}
	
	/***
	 *   生成jsp文件
	 */
	public void createJsps(){
		String dir  = StringUtil.getFirstCharLower(className);
		//main.jsp
		String fileDir_main   = "\\src\\main\\webapp\\WEB-INF\\view\\jsp\\"+dir+"\\";
		String fileName_main = "main.jsp";
		String content_main = parseMainJsp() ;
		createFile(fileDir_main,fileName_main,content_main);
		logger.info("成功创建jsp-->"+fileDir_main+fileName_main);			
	    //query.jsp
		String fileDir_query  = "\\src\\main\\webapp\\WEB-INF\\view\\jsp\\"+dir+"\\";
		String fileName_query = "query.jsp";
		String content_query = parseQueryJsp() ;
		createFile(fileDir_query,fileName_query,content_query);
		logger.info("成功创建jsp-->"+fileDir_query+fileName_query);		    
	    //list.jsp
		String fileDir_list   = "\\src\\main\\webapp\\WEB-INF\\view\\jsp\\"+dir+"\\";
		String fileName_list = "list.jsp";
		String content_list = parseListJsp() ;
		createFile(fileDir_list,fileName_list,content_list);
		logger.info("成功创建jsp-->"+fileDir_list+fileName_list);		    
	    //form.jsp
		String fileDir_form   = "\\src\\main\\webapp\\WEB-INF\\view\\jsp\\"+dir+"\\";
		String fileName_form = "form.jsp";
		String content_form = parseFormJsp() ;
		createFile(fileDir_form,fileName_form,content_form);
		logger.info("成功创建jsp-->"+fileDir_form+fileName_form);	    
	    //detail.jsp
		String fileDir_detail = "\\src\\main\\webapp\\WEB-INF\\view\\jsp\\"+dir+"\\";
		String fileName_detail = "detail.jsp";
		String content_detail = parseDetailJsp() ;
		createFile(fileDir_detail,fileName_detail,content_detail);
		logger.info("成功创建jsp-->"+fileDir_detail+fileName_detail);		    
	    
	}
	
	/***
	 * 生成Entity主体内容
	 * @param sb
	 * @param cols
	 * @return
	 */
	public String parseEntity(){
		StringBuffer sb = new StringBuffer() ;
		//生成头部内容，包名、导入jar包、类名
		sb.append("package com.cdl.entity; \n\n");
		sb.append("import java.util.Date;  \r\n");
		sb.append("import java.math.*;\r\n");
		sb.append("import com.fasterxml.jackson.annotation.JsonFormat;\r\n\r\n");
		sb.append("public class "+className+" extends BasicEntity{  \n\n\n");
		
		if(cols!=null && cols.size()>0)	{
			//生成所有属性
			for(ColumnInfo col:cols){
				if("Date".equals(col.getFieldType())){
					sb.append("@JsonFormat(pattern=\"yyyy-MM-dd\") \n");
				}
				sb.append("private \t");
				sb.append(col.getFieldType()+"  \t");
				sb.append(col.getFieldName()+"; \t //");
				sb.append(col.getColumnComments()+"\n");
			}
			sb.append("\n\n\n");
			//生成所有get/set方法
			for(ColumnInfo col:cols){
				
				//get
				sb.append("public "+col.getFieldType());
				sb.append(" get"+StringUtil.getFirstCharUpper(col.getFieldName())+"(){  \n\t");
				sb.append("return "+col.getFieldName()+"; \n");
				sb.append("}\n");
				//set
				sb.append("public void");
				sb.append(" set"+StringUtil.getFirstCharUpper(col.getFieldName())+"("+col.getFieldType()+"  "+col.getFieldName()+"){  \n\t");
				sb.append("this."+col.getFieldName()+" = "+ col.getFieldName() +"; \n");
				sb.append("}\n\n");
			}	
		}	
		sb.append("\n\n\n\r}");
		return sb.toString() ;
	}
	
	/***
	 * 生成Controller主体内容
	 * @return
	 */
	public String parseController(){
		String impl = StringUtil.getFirstCharLower(serviceImplName) ;
		String url  = StringUtil.getFirstCharLower(className);
		String _className = StringUtil.getFirstCharLower(className);
		StringBuffer sb = new StringBuffer();
		//包名
		sb.append("package com.cdl.controller; 																\n\n");
		//jar包
		sb.append("import org.slf4j.Logger;	\t\n");
		sb.append("import org.slf4j.LoggerFactory;	\t\n");
		sb.append("import org.springframework.beans.factory.annotation.Autowired;							  \n");
		sb.append("import org.springframework.stereotype.Controller;										  \n");
		sb.append("import org.springframework.web.bind.annotation.RequestMapping;							  \n");
		sb.append("import org.springframework.web.bind.annotation.ResponseBody;						      	  \n");
		sb.append("import com.cdl.entity."+ className +";													  \n");
		sb.append("import com.cdl.response.DataResponse;													  \n");
		sb.append("import com.cdl.response.FormResponse;													  \n");
		sb.append("import com.cdl.response.PageResponse;													  \n");
		sb.append("import com.cdl.response.SimpleResponse;													  \n");
		sb.append("import com.cdl.service."+ serviceName +";												  \n");
		sb.append("import com.github.pagehelper.PageInfo;												  \n\n\n");
        
		sb.append("@Controller 																				  \n");
		sb.append("@RequestMapping(\""+ url +"\")															  \n");
		sb.append("public class "+ controllerName +"{  														  \n");
		sb.append("	    private static Logger logger = LoggerFactory.getLogger("+controllerName+".class);	  \n");
		sb.append("		@Autowired																			  \n");
		sb.append("		"+serviceName+" "+impl+" ;															  \n");	
		
		sb.append(" 	/**  																				  \n");
		sb.append(" 	 * 跳转到主页面  																			  \n");
		sb.append(" 	 * @return  																		  \n");
		sb.append(" 	 */  																				  \n");
		sb.append(" 	@RequestMapping(\"/main\")  														  \n");
		sb.append(" 	public String  toMain(){  															  \n");
		sb.append(" 		logger.info(\"跳转到主页面-->"+url+"/main\");  										  \n");
		sb.append(" 		return \""+ url +"/main\";  												      \n");
		sb.append(" 	}																					  \n");
		
		sb.append(" 	/** 																				  \n");
		sb.append(" 	 * 跳转到查询页面 																		  \n");
		sb.append("	     * @return 																			  \n");
		sb.append("		 */ 																				  \n");
		sb.append("		@RequestMapping(\"/query\") 														  \n");
		sb.append("		public String  toQuery(){ 															  \n");
		sb.append("		 	logger.info(\"跳转到查询页面-->"+url+"/query\"); 									  \n");
		sb.append("		 	return \""+ url +"/query\"; 													  \n");
		sb.append("		} 																					  \n");

        sb.append("	    /**   																				  \n");
        sb.append("	     * 跳转到列表页面   																		  \n");
        sb.append("	     * @return   																		  \n");
        sb.append("      */   \n");
		sb.append("	    @RequestMapping(\"/list\")   														  \n");
		sb.append("	    	public String  toList(){   														  \n");
		sb.append("	    	logger.info(\"跳转到列表页面-->"+url+"/list\");   									  \n");
		sb.append("	    	return \""+url+"/list\";   														  \n");
		sb.append("		}   																				  \n");		

		sb.append("		/**   																				  \n");		
        sb.append("		 * 跳转到操作表单页面   																	  \n");		
        sb.append("		 * @return   																		  \n");		
        sb.append("		 */   																				  \n");		
        sb.append("		@RequestMapping(\"/form\")   														  \n");		
        sb.append("		public String toForm(){   															  \n");		
        sb.append("			logger.info(\"跳转到操作表单页面-->"+url+"/form\");   								  \n");		
        sb.append("			return \""+url+"/form\";   														  \n");		
        sb.append("		}		   																			  \n");		

		sb.append("    	/**   																				  \n");		
		sb.append("    	 * 跳转到明细页面   																		  \n");		
		sb.append("    	 * @param id   																		  \n");		
		sb.append("    	 * @return   																		  \n");		
		sb.append("    	 */   																				  \n");		
		sb.append("    	@RequestMapping(\"/detail\")   														  \n");		
		sb.append("    	public String toDetail(String id){   												  \n");		
		sb.append("    		logger.info(\"跳转到明细页面-->"+url+"/detail\");  									  \n");		
		sb.append("    		return \""+url+"/detail\";   													  \n");		
		sb.append("    	}   																				  \n");		
		
		sb.append("		/***		  																		  \n");
		sb.append("		 * 查询列表		  																	  \n");
		sb.append("		 * @param user		  																  \n");
		sb.append("		 * @return		  																	  \n");
		sb.append("		 */		  																		      \n");
		sb.append("		@ResponseBody	  																	  \n");	
		sb.append("		@RequestMapping(\"/getList\")	  													  \n");	
		sb.append("		public PageResponse<"+className+"> getList("+className+" "+_className+"){  			  \n");		
		sb.append("			logger.info(\"获取数据列表--->\"+"+_className+".toString());  						  \n");		
		sb.append("			PageInfo<"+className+"> page = "+impl+".getPage("+_className+");  				  \n");		
		sb.append("			PageResponse<"+className+"> response = new PageResponse<"+className+">() ;	  	  \n");	
		sb.append("			response.setData(page);  														  \n");		
		sb.append("			return response ;  																  \n");		
		sb.append("		}  																					  \n");
		
		sb.append("		/**																					  \n");
		sb.append("		 * 查询单条																			  \n");	
		sb.append("		 * @param "+_className+"															  \n");	
		sb.append("		 * @return																		      \n");	
		sb.append("		 */																					  \n");
		sb.append("		@ResponseBody																		  \n");	
		sb.append("		@RequestMapping(\"/get\")															  \n");	
		sb.append("		public DataResponse<"+className+"> get("+className+" "+_className+"){				  \n");	
		sb.append("			logger.info(\"获取单条数据--->\"+"+_className+".toString());						  \n");	
		sb.append("			"+className+" data = "+impl+".get("+_className+");		                          \n");
		sb.append("			DataResponse<"+className+"> response = new DataResponse<"+className+">();	      \n");	
		sb.append("			response.setData(data);		                                                      \n");
		sb.append("			return response ;		                                                          \n");
		sb.append("		}																					  \n");

		sb.append("		/*** 																	              \n");
		sb.append("		 * 新增 																			      \n");
		sb.append("		 * @param "+_className+" 															  \n");
		sb.append("		 */  																				  \n");
		sb.append("		@ResponseBody  																		  \n");
		sb.append("		@RequestMapping(\"/add\")  															  \n");
		sb.append("		public FormResponse<String> add("+className+" "+_className+"){  					  \n");
		sb.append("			logger.info(\"新增数据-->\"+"+_className+".toString());  							  \n");
		sb.append("			FormResponse<String> response = new FormResponse<String>();  		  			  \n");
		sb.append("			if(response.isSuccess()){  														  \n");
		sb.append("				"+impl+".add("+_className+");  												  \n");
		sb.append("				response.setMessage(\"新增成功！\");  											  \n");
		sb.append("			}else{ 																			  \n");
		sb.append("				response.setMessage(\"新增失败！\");  											  \n");
		sb.append("			}  																				  \n");
		sb.append("			return response;  															      \n");
		sb.append("		}  																					  \n");

		sb.append("		/***  																				  \n");
		sb.append("		 * 修改 																			      \n");
		sb.append("		 * @param "+_className+"  															  \n");
		sb.append("		 */  																				  \n");
		sb.append("		@ResponseBody  																		  \n");
		sb.append("		@RequestMapping(\"/update\")  														  \n");
		sb.append("		public FormResponse<Void> update("+className+" "+_className+"){  					  \n");
		sb.append("			logger.info(\"修改数据-->\"+"+_className+".toString());  							  \n");
		sb.append("			FormResponse<Void> response = new FormResponse<Void>();  						  \n");
		sb.append("			if(response.isSuccess()){  														  \n");
		sb.append("				"+impl+".update("+_className+");  											  \n");
		sb.append("				response.setMessage(\"修改成功！\");  											  \n");
		sb.append("			}else{  																		  \n");
		sb.append("				response.setMessage(\"修改失败！\");  											  \n");
		sb.append("			}  																				  \n");
		sb.append("			return response;  																  \n");
		sb.append("		}  																					  \n");

		sb.append("		/**  																				  \n");
		sb.append("		 * 删除  																			      \n");
		sb.append("		 * @param "+_className+"  															  \n");
		sb.append("		 */  																				  \n");
		sb.append("		@ResponseBody  																		  \n");
		sb.append("		@RequestMapping(\"/del\") 															  \n");
		sb.append("		public SimpleResponse<Void> delete("+className+" "+_className+"){  					  \n");
		sb.append("			logger.info(\"删除数据-->\"+"+_className+".toString());  							  \n");
		sb.append("			SimpleResponse<Void> response = new SimpleResponse<Void>();  					  \n");
		sb.append("			"+impl+".delete("+_className+");  												  \n");
		sb.append("			response.setSuccess(true);  													  \n");
		sb.append("			return response;  																  \n");
		sb.append("		}		 																			  \n");
		
		sb.append(" \n\t");
		sb.append(" \n\t");
		sb.append(" \n\t");
		sb.append(" \n\t");
		
		
		sb.append("}  \n\n\n");
		return sb.toString() ;
	}
	
	/***
	 * 生成Service主体内容
	 * @return
	 */
    public String parseService(){
		StringBuffer sb = new StringBuffer();
		sb.append("package com.cdl.service;     												  \n\n");
        sb.append("import com.cdl.entity."+className+";										  	  \n\n");
	    sb.append("public interface "+serviceName+" extends BasicService<"+className+">{		\n\n\n");
        sb.append("}																			    \n");	
		return sb.toString();
    }
	
    /***
     * 生成ServiceImpl主体内容
     * @return
     */
    public String parseServiceImpl(){
		StringBuffer sb = new StringBuffer();
		String _mapperName = StringUtil.getFirstCharLower(mapperName);
		sb.append("		package com.cdl.service.impl;  																		  \n\n");
					
		sb.append("		import java.util.List;																					\n");
		sb.append("		import org.slf4j.Logger;																				\n");
		sb.append("		import org.slf4j.LoggerFactory;																			\n");
		sb.append("		import org.springframework.beans.factory.annotation.Autowired;											\n");
		sb.append("		import org.springframework.stereotype.Service;															\n\n");
				
		sb.append("		import com.cdl.dao."+mapperName+";																		\n");		
		sb.append("		import com.cdl.entity."+className+";																	\n");
		sb.append("		import com.cdl.service."+serviceName+";																	\n");
		sb.append("		import com.github.pagehelper.PageInfo;																	\n");
		sb.append("		@Service(\""+_className+"ServiceImpl\")																	\n");
		sb.append("		public class "+serviceImplName+" extends BasicServiceImpl<"+className+"> implements "+serviceName+"{	\n");				
		sb.append("			private static Logger logger = LoggerFactory.getLogger("+serviceImplName+".class);				  \n\n");
				
	    sb.append("			@Autowired																							\n");
		sb.append("			"+mapperName+" "+_mapperName+" ;																  \n\n");		

		sb.append("			@Override																							\n");						
		sb.append("			public List<"+className+"> getList("+className+" "+_className+") {									\n");		
		sb.append("				logger.info(\"查询用户列表\");																		\n");									
		sb.append("				List<"+className+"> u = "+_mapperName+".getList("+_className+");								\n");	
		sb.append("				return u;																						\n");						
		sb.append("			}																								  \n\n");	

		sb.append("			@Override																							\n");		
		sb.append("			public "+className+" get("+className+" "+_className+") {											\n");
		sb.append("				"+className+" u = "+_mapperName+".get("+_className+");											\n");
		sb.append("				return u;																						\n");			
		sb.append("			}																								  \n\n");

		sb.append("			@Override																							\n");
		sb.append("			public void add("+className+" "+_className+") {														\n");	
		sb.append("				"+_mapperName+".add("+_className+");															\n");
		sb.append("			}																								  \n\n");					

		sb.append("			@Override																							\n");	
		sb.append("			public void update("+className+" "+_className+") {													\n");			
		sb.append("				"+_mapperName+".update("+_className+");															\n");		
		sb.append("			}																								  \n\n");					

		sb.append("			@Override																							\n");						
		sb.append("			public void delete("+className+" "+_className+") {													\n");
		sb.append("				"+_mapperName+".delete("+_className+");															\n");						
		sb.append("			}																								  \n\n");					

		sb.append("			@Override																							\n");				
		sb.append("			public PageInfo<"+className+"> getPage("+className+" "+_className+") {								\n");	
		sb.append("				return getPage("+_className+".getPageNum(),"+_className+".getPageSize(),"+_className+");		\n");			
		sb.append("			}																									\n");			
		sb.append("		}																									  \n\n");
		return sb.toString();
    }

    /***
     * 生成Mapper主体内容
     * @return
     */
    public String parseMapper(){
		StringBuffer sb = new StringBuffer();
		sb.append("package com.cdl.dao;     													\n\n");
        sb.append("import com.cdl.entity."+className+";										    \n\n");
	    sb.append("public interface "+mapperName+" extends BasicMapper<"+className+">{		  \n\n\n");
	    sb.append("}																			  \n");
	    return sb.toString();
    }
  
    /***
     * 生成sqlMap主体内容
     * @return
     */
    public String parseSqlMap(){
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>    							\n");   
		sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"   			\n");
		sb.append("	\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >   						\n");
		sb.append("<mapper namespace=\"com.cdl.dao."+ mapperName +"\" >  						\n");
				
		sb.append("		  <!-- 查询数据字段 -->															\n");
		sb.append("		  <sql id=\"selectSql\">													\n");		
		sb.append("		  	   select  																\n");
		for(int i=0;i<cols.size();i++){
			ColumnInfo col = cols.get(i);
			if(i==0){
				sb.append("			   		  "+col.getColumnName()+" "+StringUtil.getFiledString(col.getColumnName())+"	\n");			
			}else{
				sb.append("			   		, "+col.getColumnName()+" "+StringUtil.getFiledString(col.getColumnName())+"	\n");
			}
		}			
		sb.append("			   from "+tableName+"													\n");	
		sb.append("</sql>																		  \n\n");
				
		sb.append("		  <!-- 主键作为唯一条件查询 -->														\n");
		sb.append("		  <sql id=\"whereSqlByKey\">												\n");	
		sb.append("		  	 <where> 																\n");
		for(String cn:primaryCol){
			sb.append("				and	"+ cn +" = #{"+ StringUtil.getFiledString(cn) +"}			\n");				
		}
		sb.append("		     </where> 																\n");
		sb.append("		  </sql>																  \n\n");
				
		sb.append("		  <!-- 多条件组合查询 -->															\n");
		sb.append("		  <sql id=\"whereSql\">														\n");
		sb.append("		  	 <where> 																\n");
		for(ColumnInfo col:cols){
			sb.append("		  	   <if test=\""+ StringUtil.getFiledString(col.getColumnName()) +"   != null   and "+ StringUtil.getFiledString(col.getColumnName()) +" !=''\"> and "+col.getColumnName()+" = #{"+StringUtil.getFiledString(col.getColumnName())+"} </if>   \n");
		}			
		sb.append("		     </where> 																\n");	
		sb.append("		  </sql>   																  \n\n");
				
		sb.append("		  <!-- 获取列表 -->																\n");	 
		sb.append("		  <select id=\"getList\" parameterType=\""+className+"\" resultType=\""+className+"\">	\n");			
		sb.append("			   <include refid=\"selectSql\"></include>								\n");
		sb.append("			   <include refid=\"whereSql\"></include>								\n");
		sb.append("		  </select>																  \n\n");
				
		sb.append("		  <!-- 获取单条信息 -->															\n");	
		sb.append("		  <select id=\"get\" parameterType=\""+className+"\" resultType=\""+className+"\">		\n");		
		sb.append("			   <include refid=\"selectSql\"></include>								\n");
		sb.append("			   <include refid=\"whereSqlByKey\"></include>							\n");
		sb.append("		  </select>																  \n\n");
				
		sb.append("		  <!-- 新增 -->																\n");
		sb.append("		  <insert id=\"add\" parameterType=\""+className+"\">						\n");		
		sb.append("		       insert into "+tableName+"(											\n");
		sb.append("		       <trim prefix=\"\" prefixOverrides=\",\">								\n");
		for(ColumnInfo col:cols){
			sb.append("		       	  <if test=\""+ StringUtil.getFiledString(col.getColumnName()) +" != null\">,"+ col.getColumnName() +"</if>   \n");
		}			
		sb.append("		       </trim>																\n");
		sb.append("		       )values(																\n");
		sb.append("		       <trim prefix=\"\" prefixOverrides=\",\">								\n");			
		for(ColumnInfo col:cols){
			sb.append("		       	  <if test=\""+ StringUtil.getFiledString(col.getColumnName()) +" != null\">,#{"+ StringUtil.getFiledString(col.getColumnName()) +"}</if>   \n");
		}			
		sb.append("		       </trim>																\n");	
		sb.append("		       )																	\n");
		sb.append("		  </insert> 															  \n\n");
				
		sb.append("		  <!-- 修改 -->																\n");
		sb.append("		  <update id=\"update\" parameterType=\""+className+"\">					\n");			
		sb.append("		      update "+tableName+"													\n");
		sb.append("		      <set>																	\n");
		for(ColumnInfo col:cols){
			sb.append("		      	 "+ col.getColumnName() +"   =  #{"+ StringUtil.getFiledString(col.getColumnName()) +",jdbcType="+ col.getJdbcType() +"},   \n");
		}				
		sb.append("		      </set>																\n");
		sb.append("		      <include refid=\"whereSqlByKey\"></include>							\n");		
		sb.append("		  </update>  															  \n\n");
				
		sb.append("		  <!-- 删除 -->																\n");
		sb.append("		  <delete id=\"delete\" parameterType=\""+className+"\">					\n");			
		sb.append("		      delete from "+tableName+" 											\n");
		sb.append("		      <include refid=\"whereSqlByKey\"></include>							\n");
		sb.append("		  </delete>																  \n\n");
				
		sb.append("</mapper>    																	\n");
		return sb.toString();    	
    }
    
	/***
	 * 生成模块主页面
	 * @return
	 */
    public String parseMainJsp(){
    	String url  = StringUtil.getFirstCharLower(className);
    	StringBuffer sb = new StringBuffer() ;
		sb.append("<%@ page language=\"java\" contentType=\"text/html;charset=UTF-8\" pageEncoding=\"UTF-8\" isELIgnored=\"false\"%>   																			\n");
		sb.append("<%@ include file=\"/WEB-INF/view/jsp/base.jsp\" %>  																																			\n");
	    sb.append("<html lang=\"en\">    																																										\n");
	    sb.append("<body class=\"wysihtml5-supported\">    																																						\n");
	    sb.append("	<div id=\"content\">   																																										\n");
	    sb.append("		<div id=\"content-header\">  																																							\n");
	    sb.append("			<div class=\"form-actions\">    																																					\n");
	    sb.append("              <button type=\"button\" class=\"btn btn-success\" url=\"<%=path%>/"+ url +"/query\"  onclick=\"C.toUrl($(this),'"+ url +"-iframe-main')\">查询</button>							\n");
	    sb.append("              <button type=\"button\" class=\"btn btn-primary\" url=\"<%=path%>/"+ url +"/form\"   onclick=\"C.toUrl($(this),'"+ url +"-iframe-main')\">新增</button>							\n");
	    sb.append("              <button type=\"button\" class=\"btn btn-info\"    url=\"<%=path%>/"+ url +"/list\"   onclick=\"C.toUrl($(this),'"+ url +"-iframe-main')\">全列表</button>  						\n");
	    sb.append("            </div>   																																										\n");
	    sb.append("		</div>   																																												\n");
	    sb.append("		<iframe src=\"<%=path %>/"+ url +"/query\" id=\""+ url +"-iframe-main\" frameborder=\"0\" style=\"width: 100%; height: 553px;\"></iframe>   											\n");
	    sb.append("	</div>  																																													\n");
	    sb.append("</body>  																																													\n");
	    sb.append("</html>																																														\n");	
		
		return sb.toString() ;
    }
    
    /***
     * 生成查询页面
     * @return
     */
    public String parseQueryJsp(){
    	String url  = StringUtil.getFirstCharLower(className);
    	StringBuffer sb = new StringBuffer() ;
	    sb.append("<%@ page language=\"java\" contentType=\"text/html;charset=UTF-8\" pageEncoding=\"UTF-8\" isELIgnored=\"false\"%>																		\n");
	    sb.append("<%@ include file=\"/WEB-INF/view/jsp/base.jsp\" %>                                                                                                                                       \n");
	    sb.append("<html lang=\"en\">                                                                                                                                                                       \n");
	    sb.append("<body class=\"wysihtml5-supported\">                                                                                                                                                     \n");
	    sb.append("	<div id=\"content\">                                                                                                                                                                    \n");
	    for(IndexInfo idx:indexs){
		    String[] colNames = idx.getColNames().split(",") ;
		    sb.append("		<div class=\"container-fluid\" >                                                                                                                                                      \n");
		    sb.append("			<div class=\"row-fluid\">                                                                                                                                                           \n");
		    sb.append("				<div class=\"span6\">                                                                                                                                                             \n");
		    sb.append("					<div class=\"widget-box\">                                                                                                                                                      \n");
		    sb.append("						<div class=\"widget-title\">                                                                                                                                                  \n");
		    sb.append("							<h5>查询</h5>                                                                                                                                                               \n");
		    sb.append("						</div>                                                                                                                                                                        \n");
		    sb.append("						<div class=\"widget-content nopadding\">                                                                                                                                      \n");
		    //判断是否唯一索引
		    if(idx.isUnique()){
			    sb.append("							<form id=\"\" action=\"<%=path %>/"+url+"/detail\" method=\"post\" onsubmit=\"return C.toVail($(this))\" class=\"form-horizontal\">                                   \n");		    	
		    }else{
			    sb.append("							<form id=\"\" action=\"<%=path %>/"+url+"/list\" method=\"post\" onsubmit=\"return C.toVail($(this))\" class=\"form-horizontal\">                                   \n");		    	
		    }
		    //索引字段
		    for(int i=0;i<colNames.length;i++){
			    String filedName =  StringUtil.getFiledString(colNames[i]);
			    String comments = null ;
			    for(ColumnInfo c:cols){
			    	if(colNames[i].equals(c.getColumnName())){
			    		comments = c.getColumnComments() ;
			    		break;
			    	}
			    }
		    	sb.append("								<div class=\"control-group\">                                                                                                                                             \n");
			    sb.append("									<label class=\"control-label\">"+comments+" :</label>                                                                                                                         \n");
			    sb.append("									<div class=\"controls\">                                                                                                                                                \n");
			    sb.append("										<input type=\"text\" class=\"span11\" name=\""+filedName+"\" placeholder=\"\">                                                                                         \n");
			    sb.append("										<span class=\"error_msg\" name=\"error_"+filedName+"\"></span>                                                                                                               \n");
			    sb.append("									</div>                                                                                                                                                                  \n");
			    sb.append("								</div>                                                                                                                                                                    \n");
		    }
		    sb.append("								<div class=\"form-actions\">                                                                                                                                              \n");
		    sb.append("									<button type=\"submit\" class=\"btn btn-success\" style=\"float:right\">查询</button>                                                                                   \n");
		    sb.append("								</div>                                                                                                                                                                    \n");
		    sb.append("							</form>                                                                                                                                                                     \n");
		    sb.append("						</div>                                                                                                                                                                        \n");
		    sb.append("					</div>                                                                                                                                                                          \n");
		    sb.append("				</div>                                                                                                                                                                            \n");
		    sb.append("			</div>                                                                                                                                                                              \n");
		    sb.append("		</div>                                                                                                                                                                                \n");
	    }
	                                                                                                                                                                                  
	    sb.append("	</div>                                                                                                                                                                                  \n");
	    sb.append("</body>                                                                                                                                                                                  \n");
	    sb.append("	<script>                                                                                                                                                                                \n");
	    sb.append("	$(document).ready(function(){                                                                                                                                                           \n");
	    sb.append("		var result = \"${param.result}\";                                                                                                                                                     \n");
	    sb.append("		if(result==\"null\"){                                                                                                                                                                 \n");
	    sb.append("		    C.showMsg(\"无查询结果\");                                                                                                                                                            \n");
	    sb.append("		}                                                                                                                                                                                     \n");
	    sb.append("	});                                                                                                                                                                                     \n");
	    sb.append("	</script>                                                                                                                                                                               \n");
	    sb.append("</html>		                                                                                                                                                                              \n");
		return sb.toString() ;    	
    }
    
    /***
     * 生成列表页面
     * @return
     */
    public String parseListJsp(){
    	String url  = StringUtil.getFirstCharLower(className);
    	StringBuffer sb = new StringBuffer() ;
	    sb.append("<%@ page language=\"java\" contentType=\"text/html;charset=UTF-8\" pageEncoding=\"UTF-8\" isELIgnored=\"false\"%>																			\n");
	    sb.append("<%@ include file=\"/WEB-INF/view/jsp/base.jsp\" %>																																			\n");
	    sb.append("<html lang=\"en\">																																											\n");
	    sb.append("<body>																																														\n");
	    sb.append("	<div id=\"content\">																																										\n");
	    sb.append("		<div id=\"content-header\"></div>																																						\n");
	    sb.append("		<div class=\"container-fluid\">																																							\n");
	    sb.append("			<div class=\"widget-box\">																																							\n");
	    sb.append("				<div class=\"widget-title\">																																					\n");
	    sb.append("					<h5>列表</h5>																																									\n");
	    sb.append("			    </div>																																											\n");
	    sb.append("				<div class=\"widget-content nopadding\">																																		\n");
	    sb.append("					<table id=\""+url+"Table\" url=\"<%=path%>/"+url+"/getList\" class=\"table table-bordered data-table \">																	\n");
	    sb.append("						<thead>																																									\n");
	    sb.append("							<tr role=\"row\">																																					\n");
	    for(ColumnInfo col:cols){
	    	String fieldName = StringUtil.getFiledString(col.getColumnName()) ;
	    	String comments =  col.getColumnComments() ;
			sb.append("								<th class=\"ui-state-default\" role=\"columnheader\"  style=\"\" name=\""+fieldName+"\" "+ (Arrays.asList(primaryKey).contains(fieldName)?"primaryKey=\"true\"":"") +">"+comments+"</th> 										\n");
	    }
	    sb.append("								<th class=\"ui-state-default\" role=\"columnheader\"  style=\"\" name=\"work-box\"></th>																\n");
	    sb.append("							</tr>																																								\n");
	    sb.append("						</thead>																																								\n");
	    sb.append("						<tbody role=\"alert\" aria-live=\"polite\" aria-relevant=\"all\">																										\n");
	    sb.append("						</tbody>																																								\n");
	    sb.append("					</table>																																									\n");
	    sb.append("				</div>																																											\n");
	    sb.append("			</div>																																												\n");
	    sb.append("		</div>																																													\n");
	    sb.append("	</div>																																														\n");
	    sb.append("</body>																																														\n");
	    sb.append("     <script type=\"text/javascript\">																																						\n");
	    sb.append("    	    var param = C.getParams(\"${param}\"); //接收传参																																		\n");
	    sb.append("    	    //加载数据																																												\n");
	    sb.append("    	    toPage();																																											\n");

	    sb.append("    	    //分页函数																																												\n");
	    sb.append("    	    function toPage(pageNum){																																							\n");
	    sb.append("    	    	if(pageNum == 0){																																								\n");
	    sb.append("    	    		return ;																																									\n");
	    sb.append("    	    	}																																												\n");
	    sb.append("    	    	param.pageNum = pageNum?pageNum:1;																																				\n");
	    sb.append("    	    	C.load($(\"#"+url+"Table\"),param);																																				\n");
	    sb.append("    	    }																																													\n");

	    sb.append("     	    //编辑																																											\n");
	    sb.append("     		function edit(_this){																																							\n");
	    sb.append("					var form = _this.siblings(\"form\");	    																																\n");	
	    sb.append("					form.attr(\"action\",\"<%=path%>/"+url+"/form?pageNum=\"+param.pageNum);																									\n");
	    sb.append("					form.submit();	    																																						\n");	
	    sb.append("     		}   																																											\n");

	    sb.append("     		//删除																																											\n");
	    sb.append("     		function del(_this){																																							\n");
	    sb.append("					var form = _this.siblings(\"form\");																																		\n");
	    sb.append("     			var _tr = _this.parent().parent();																																			\n");
	    sb.append("     			var sib = _tr.siblings(\"tr\").length;																																		\n");
	    sb.append("     			if(confirm(\"确认删除？\")){																																					\n");
	    sb.append("         			$.ajax({																																								\n");
	    sb.append("         				url:\"<%=path %>/"+url+"/del\",																																		\n");
	    sb.append("         				data:form.serialize(),																																				\n");
	    sb.append("         				type:\"post\",																																						\n");
	    sb.append("         				success:function(text){																																				\n");
	    sb.append("         					if(text.success){																																				\n");
	    sb.append("         						C.showMsg(\"删除成功！\");																																			\n");
	    sb.append("         						if(sib==0){																																					\n");
	    sb.append("             						//删除完成刷新当前页（删除的数据为当前页唯一条时，pageNum-1）																											\n");
	    sb.append("             						_tr.remove();  //如果删除的记录为当前结果集的唯一一条，前后台同时做删除操作																									\n");
	    sb.append("             						toPage((param.pageNum-1)>0?param.pageNum-1:1);																											\n");
	    sb.append("             					}else{																																						\n");
	    sb.append("             						//删除完成刷新当前页																																			\n");
	    sb.append("             						toPage(param.pageNum);																																	\n");
	    sb.append("             					}																																							\n");
	    sb.append("         					}else{																																							\n");
	    sb.append("         						C.showMsg(\"删除失败！\");																																			\n");
	    sb.append("         					}																																								\n");
	    sb.append("         				}																																									\n");
	    sb.append("         			});																																										\n");
	    sb.append("     			}																																											\n");
	    sb.append("     		}																																												\n");
	    sb.append("     </script>																																												\n");
	    sb.append("</html>	    																																												\n");
		
		return sb.toString() ;    	
    }
    
    /***
     * 生成表单页面
     * @return
     */
    public String parseFormJsp(){
    	String url  = StringUtil.getFirstCharLower(className);
    	StringBuffer sb = new StringBuffer() ;
	    sb.append("<%@ page language=\"java\" contentType=\"text/html;charset=UTF-8\" pageEncoding=\"UTF-8\" isELIgnored=\"false\"%>																			\n");
	    sb.append("<%@ include file=\"/WEB-INF/view/jsp/base.jsp\" %>																																			\n");
	    sb.append("<html lang=\"en\">																																											\n");
	    sb.append("<body class=\"wysihtml5-supported\">																																							\n");
	    sb.append("	<div id=\"content\">																																										\n");
	    sb.append("		<div class=\"container-fluid\">																																							\n");
	    sb.append("			<div class=\"row-fluid\">																																							\n");	
	    sb.append("				<div class=\"span6\">																																							\n");
	    sb.append("					<div class=\"widget-box\">																																					\n");
	    sb.append("						<div class=\"widget-title\">																																			\n");
	    sb.append("							<h5>表单</h5>																																							\n");
	    sb.append("						</div>																																									\n");
	    sb.append("						<div class=\"widget-content nopadding\">																																\n");
	    sb.append("							<form id=\""+ url +"Form\" action=\"\" method=\"post\" class=\"form-horizontal\">																					\n");
	    for(ColumnInfo col:cols){
		    String fieldName = StringUtil.getFiledString(col.getColumnName());
		    String fieldType = col.getFieldType(); 
		    String comments =  col.getColumnComments() ;
	    	sb.append("								<div class=\"control-group\">																																\n");
		    sb.append("									<label class=\"control-label\">"+comments+" :</label>																									\n");
		    sb.append("									<div class=\"controls\">																																\n");
		    sb.append("										<input "+ (Arrays.asList(primaryKey).contains(fieldName)?" primaryKey=\"true\"":"") +" type=\""+ ("Date".equals(fieldType)?"date":"text") +"\" class=\"span11\" name=\""+fieldName+"\" placeholder=\"\">		\n");
		    sb.append("										<span class=\"error_msg\" name=\"error_"+fieldName+"\"></span> 																						\n");
		    sb.append("									</div>																																					\n");
		    sb.append("								</div>																																						\n");
	    }
	    sb.append("								<div class=\"form-actions\">																																	\n");
	    sb.append("									<button type=\"button\" class=\"btn btn-success f-right\" style=\"float:right\" onclick=\"save()\">保存</button>												\n");
	    sb.append("								</div>																																							\n");
	    sb.append("							</form>																																								\n");
	    sb.append("						</div>																																									\n");
	    sb.append("					</div>																																										\n");
	    sb.append("				</div>																																											\n");
	    sb.append("			</div>																																												\n");
	    sb.append("		</div>																																													\n");
	    sb.append("	</div>																																														\n");
	    sb.append("	<script>																																													\n");
	    sb.append("	    var params = C.getParams(\"${param}\");   //接收传来的参数																																							\n");
	    sb.append("	    var _pageNum = \"\" ;  //接收list页面传来的当面页数																																				\n");
	    sb.append("		//初始化表单数据																																												\n");
	    sb.append("		$(document).ready(function(){																																							\n");
	    sb.append("			_pageNum = \"${param.pageNum}\" ;																																					\n");
	    sb.append("			if(!$.isEmptyObject(params)){																																						\n");
	    sb.append("				$(\"input[primaryKey='true']\").attr(\"readonly\",\"readonly\");																												\n");
	    sb.append("				$.ajax({																																										\n");
	    sb.append("					url:\"<%=path%>/"+ url +"/get\",																																			\n");
	    sb.append("				    data:params,																																						\n");
	    sb.append("				    type:\"post\",																																								\n");
	    sb.append("				    success:function(text){																																						\n");
	    sb.append("				    	if(text.success){																																						\n");
	    sb.append("				    		$(\"#"+url+"Form\").autofill(text.data);																															\n");
	    sb.append("				    	}																																										\n");
	    sb.append("				    }																																											\n");
	    sb.append("				});																																												\n");
	    sb.append("			}																																													\n");
	    sb.append("		});																																														\n");

	    sb.append("		//保存表单数据																																												\n");
	    sb.append("		function save(){																																										\n");
	    sb.append("			var _editType = null ;																																								\n");
	    sb.append("			if(!$.isEmptyObject(params)){																																						\n");
	    sb.append("				_editType = \"edit\";																																							\n");
	    sb.append("				$(\"#"+ url +"Form\").prop(\"action\",\"<%=path %>/"+ url +"/update\");																					\n");
	    sb.append("			}else{																																												\n");
	    sb.append("				_editType = \"add\";																																							\n");
	    sb.append("				$(\"#"+ url +"Form\").prop(\"action\",\"<%=path %>/"+ url +"/add\");																						\n");
	    sb.append("			}																																													\n");
	    sb.append("			//保存																																												\n");
	    sb.append("			$(\"#"+ url +"Form\").ajaxSubmit(function(text){																															\n");
	    sb.append("			    C.showMsg(text.message);																																							\n");
	    sb.append("			    if(text.success){																																								\n");
	    sb.append("			    	if(_editType == \"edit\"){																																					\n");
	    sb.append("			    		window.history.go(-1); //返回原页面																																			\n");
	    sb.append("			    	}else if(_editType == \"add\"){																																				\n");
	    sb.append("						$(\"#"+ url +"Form\").prop(\"action\",\"<%=path %>/"+ url +"/detail\");																											\n");
	    sb.append("						$(\"#"+ url +"Form\").submit();																																				\n");
	    sb.append("			    	}																																											\n");
	    sb.append("				}																																												\n");
	    sb.append("			});																																													\n");
	    sb.append("		}																																														\n");
	    sb.append("	</script>																																													\n");
	    sb.append("</body>																																														\n");
	    sb.append(" </html>	    																																												\n");
		
		return sb.toString() ;    	
    }
    /***
     * 生成详情页面
     * @return
     */
    public String parseDetailJsp(){
    	String url  = StringUtil.getFirstCharLower(className);
    	StringBuffer sb = new StringBuffer() ;
	    sb.append("<%@ page language=\"java\" contentType=\"text/html;charset=UTF-8\" pageEncoding=\"UTF-8\" isELIgnored=\"false\"%>																			\n");
	    sb.append("<%@ include file=\"/WEB-INF/view/jsp/base.jsp\" %>																																			\n");
	    sb.append("<html lang=\"en\">																																											\n");
	    sb.append("<body class=\"wysihtml5-supported\">																																							\n");
	    sb.append("	<div id=\"content\">																																										\n");
	    sb.append("		<div class=\"container-fluid\">																																							\n");
	    sb.append("			<div class=\"row-fluid\">																																							\n");
	    sb.append("				<div class=\"span6\">																																							\n");
	    sb.append("					<div class=\"widget-box\">																																					\n");
	    sb.append("						<div class=\"widget-title\">																																			\n");
	    sb.append("							<h5>详情</h5>																																							\n");
	    sb.append("						</div>																																									\n");
	    sb.append("						<div class=\"widget-content nopadding\">																																\n");
	    sb.append("							<form id=\""+ url +"Form\" action=\"\" method=\"post\" class=\"form-horizontal\">																					\n");
	    //设置主键隐藏字段
	    for(String cp:primaryKey){
		    sb.append("								<input type=\"hidden\" name="+cp+" />																																\n");	    	
	    }
	    for(ColumnInfo col:cols){
	    	String comments =  col.getColumnComments() ;
	    	String fieldName = StringUtil.getFiledString(col.getColumnName());
		    sb.append("								<div class=\"control-group\">																																\n");
    	    sb.append("									<label class=\"control-label\">"+ comments +" :</label>																								\n");
    	    sb.append("									<div class=\"controls\">																																\n");
    	    sb.append("										<span class=\"span11\" name=\""+ fieldName +"\" ></span>																							\n");
    	    sb.append("									</div>																																					\n");
    	    sb.append("								</div>	    																																				\n");
	    }		
	    sb.append("								<div class=\"form-actions\">																																	\n");
	    sb.append("									<button type=\"button\" class=\"btn btn-success\" style=\"float:right;margin-left:10px;\"  onclick=\"del($(this))\">删除</button>								\n");
	    sb.append("									<button type=\"button\" class=\"btn btn-success\" style=\"float:right;margin-left:10px;\"  onclick=\"edit($(this))\">修改</button>							\n");
	    sb.append("								</div>																																							\n");
	    sb.append("							</form>																																								\n");
	    sb.append("						</div>																																									\n");
	    sb.append("					</div>																																										\n");
	    sb.append("				</div>																																											\n");
	    sb.append("			</div>																																												\n");
	    sb.append("		</div>																																													\n");
	    sb.append("	</div>																																														\n");
	    sb.append("	<script>																																													\n");
	    sb.append("	    var params = C.getParams(\"${param}\");   //接收传来的参数																																												\n");
	    sb.append("		//初始化表单数据																																												\n");
	    sb.append("		$(document).ready(function(){																																							\n");
	    sb.append("																																																\n");
	    sb.append("			if(!$.isEmptyObject(params)){																																						\n");
	    sb.append("				$.ajax({																																										\n");
	    sb.append("					url:\"<%=path%>/"+ url +"/get\",																																			\n");
	    sb.append("				    data:params,																																			\n");
	    sb.append("				    type:\"post\",																																								\n");
	    sb.append("				    success:function(text){																																						\n");
	    sb.append("				    	if(text.success){																																						\n");
	    sb.append("							$(\"#"+ url +"Form\").autofill(text.data);   //填充隐藏表单																												\n");
	    sb.append("				    		var _data = text.data ;																																				\n");
	    sb.append("					    	$(\"#"+ url +"Form\").find(\"span\").each(function(){																												\n");
	    sb.append("					    		_this = $(this) ;																																				\n");
	    sb.append("					    		_name = _this.attr(\"name\") ;																																	\n");
	    sb.append("					    		if(_name!=null){																																				\n");
	    sb.append("					    			for(key in _data){																																			\n");
	    sb.append("					    				if(_name == key){																																		\n");
	    sb.append("					    					_this.html(_data[key]);																																\n");
	    sb.append("					    				}																																						\n");
	    sb.append("					    			}																																							\n");
	    sb.append("					    		}																																								\n");
	    sb.append("					        });																																									\n");
	    sb.append("				    	}else{																																									\n");
	    sb.append("				    		//查询无结果时返回原查询页面，并返回标识result=null表示无查询结果																														\n");
	    sb.append("				    		window.location.href=\"<%=path%>/"+ url +"/query?result=null\";																										\n");
	    sb.append("				    	}																																										\n");
	    sb.append("				    }																																											\n");
	    sb.append("				});																																												\n");
	    sb.append("			}																																													\n");
	    sb.append("		});																																														\n");

	    sb.append("	    	//编辑																																													\n");
	    sb.append("			function edit(_this){																																									\n");
	    sb.append("             $(\"#"+ url +"Form\").attr(\"action\",\"<%=path%>/"+ url +"/form\");																												\n");
	    sb.append("			    $(\"#"+ url +"Form\").submit();	    																																			\n");
	    sb.append(" 		}   																																												\n");

	    sb.append(" 		//删除																																												\n");
	    sb.append(" 		function del(_this){																																								\n");
	    sb.append(" 			if(confirm(\"确认删除？\")){																																						\n");
	    sb.append("				$(\"#"+ url +"Form\").attr(\"action\",\"<%=path%>/"+ url +"/form\");																												\n");
	    sb.append("    			$.ajax({																																										\n");
	    sb.append("     				url:\"<%=path %>/"+ url +"/del\",																																		\n");
	    sb.append("     				data:$(\"#"+ url +"Form\").serialize(),																																					\n");
	    sb.append("     				type:\"post\",																																							\n");
	    sb.append("     				success:function(text){																																					\n");
	    sb.append("     					if(text.success){																																					\n");
	    sb.append("     						alert(\"删除成功！\");																																				\n");
	    sb.append("     						history.go(-1);																																					\n");
	    sb.append("     					}else{																																								\n");
	    sb.append("     						alert(\"删除失败！\");																																				\n");
	    sb.append("     					}																																									\n");
	    sb.append("     				}																																										\n");
	    sb.append("     			});																																											\n");
	    sb.append(" 			}																																												\n");
	    sb.append(" 		}																																													\n");
	    sb.append("	</script>																																													\n");
	    sb.append("</body>																																														\n");
	    sb.append("</html>	    																																												\n");
		
		return sb.toString() ;    	
    }
    
    /***
	 * 根据字段类型设置对应的bean属性和类型
	 * @param colType
	 * @return
	 */
	public  List<ColumnInfo> initColumnInfo(List<ColumnInfo> cols){
		for(ColumnInfo c:cols){
			c.setFieldName(StringUtil.getFiledString(c.getColumnName()));
			int len = c.getColumnLength() ;
			int scale = c.getColumnScale() ;
			if("INTEGER".equals(c.getColumnType())){
				c.setFieldType("Integer");
				c.setJdbcType("NUMERIC");
			}else if( "LONG".equals(c.getColumnType())){
				c.setFieldType("Long") ;
				c.setJdbcType("NUMERIC");
			}else if("NUMBER".equals(c.getColumnType())){
				c.setFieldType(scale==0?(len<10?"Integer":(len<18?"Long":"BigDecimal")):"BigDecimal");
				c.setJdbcType("NUMERIC");
			}else if( "FLOAT".equals(c.getColumnType())
					||"FLOAT PRECISION".equals(c.getColumnType())
					||"DOUBLE".equals(c.getColumnType())
					||"BINARY_DOUBLE".equals(c.getColumnType())
					||"DOUBLE PRECISION".equals(c.getColumnType())
					){
				c.setFieldType("BigDecimal");
				c.setJdbcType("NUMERIC");
			}else if( "DECIMAL".equals(c.getColumnType())
					||"REAL".equals(c.getColumnType())
					){
				c.setFieldType(scale==0?(c.getColumnLength()<10?"Integer":"Long"):"BigDecimal");
				c.setJdbcType("NUMERIC");
			}else if( "VARCHAR".equals(c.getColumnType())
					||"VARCHAR2".equals(c.getColumnType())
					||"CHAR".equals(c.getColumnType())
					||"NVARHCAR".equals(c.getColumnType())
					||"NVARCHAR2".equals(c.getColumnType())
					||"NCHAR".equals(c.getColumnType())
					){
				c.setFieldType("String");
				c.setJdbcType("NVARCHAR");
			}else if( "DATETIME".equals(c.getColumnType())
					||"DATE".equals(c.getColumnType())
					||c.getColumnType().contains("TIMESTAMP")
					){
				c.setFieldType("Date") ;
				c.setJdbcType("DATE");
			}
		}
		return cols ;
	}
	
	
	public String getJdbcType(String type){
		
		return null ;
	}
	
	
	public void closeWrite(	FileWriter fw,PrintWriter pw){
		try {		 
			if(fw!=null){
			  fw.close();
			}
			if(pw!=null){
			  pw.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		
		System.out.println(System.getProperty("user.dir"));
		CodeUtil u = new CodeUtil("SYS_PARA");
		u.createEntity();
	    u.createMapper();
	    u.createService();
	    u.createServiceImple();
	    u.createSqlMap();
		u.createController();
		u.createJsps();
	
		
	
	}

}
